package com.example.stuwork.controller;

import com.example.stuwork.common.Result;
import com.example.stuwork.pojo.ClassCourse;
import com.example.stuwork.service.ClassCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级课程关联控制器
 */
@RestController
@RequestMapping("/class-course")
public class ClassCourseController {
    
    @Autowired
    private ClassCourseService classCourseService;
    
    /**
     * 根据课程ID获取关联的班级课程信息
     *
     * @param courseId 课程ID
     * @return 班级课程关联列表
     */
    @GetMapping("/course/{courseId}")
    public Result<List<ClassCourse>> getByCourseId(@PathVariable String courseId) {
        List<ClassCourse> classCourses = classCourseService.findByCourseId(courseId);
        return Result.success("获取课程关联班级信息成功", classCourses);
    }
    
    /**
     * 根据班级ID获取关联的班级课程信息
     *
     * @param classId 班级ID
     * @return 班级课程关联列表
     */
    @GetMapping("/class/{classId}")
    public Result<List<ClassCourse>> getByClassId(@PathVariable Long classId) {
        List<ClassCourse> classCourses = classCourseService.findByClassId(classId);
        return Result.success("获取班级关联课程信息成功", classCourses);
    }
    
    /**
     * 根据教师ID获取关联的班级课程信息
     *
     * @param teacherId 教师ID
     * @return 班级课程关联列表
     */
    @GetMapping("/teacher/{teacherId}")
    public Result<List<ClassCourse>> getByTeacherId(@PathVariable String teacherId) {
        List<ClassCourse> classCourses = classCourseService.findByTeacherId(teacherId);
        return Result.success("获取教师授课班级信息成功", classCourses);
    }
    
    /**
     * 根据学期获取班级课程信息
     *
     * @param term 学期
     * @return 班级课程关联列表
     */
    @GetMapping("/term/{term}")
    public Result<List<ClassCourse>> getByTerm(@PathVariable String term) {
        List<ClassCourse> classCourses = classCourseService.findByTerm(term);
        return Result.success("获取学期班级课程信息成功", classCourses);
    }
    
    /**
     * 添加班级课程关联
     *
     * @param classCourse 班级课程关联信息
     * @return 操作结果
     */
    @PostMapping
    public Result<Boolean> addClassCourse(@RequestBody ClassCourse classCourse) {
        boolean success = classCourseService.addClassCourse(classCourse);
        if (success) {
            return Result.success("添加班级课程关联成功", true);
        } else {
            return Result.error("添加班级课程关联失败");
        }
    }
    
    /**
     * 批量分配班级到课程
     *
     * @param courseId  课程ID
     * @param request   包含班级ID列表、教师ID和学期的请求
     * @return 操作结果
     */
    @PostMapping("/assign/{courseId}")
    public Result<Boolean> assignClassesToCourse(
            @PathVariable String courseId,
            @RequestBody AssignClassesRequest request) {
        
        if (request == null || request.getClassIds() == null || request.getClassIds().isEmpty()) {
            return Result.error("班级ID列表不能为空");
        }
        
        if (request.getTeacherId() == null || request.getTeacherId().trim().isEmpty()) {
            return Result.error("教师ID不能为空");
        }
        
        boolean success = classCourseService.assignClassesToCourse(
                courseId,
                request.getClassIds(),
                request.getTeacherId(),
                request.getTerm());
        
        if (success) {
            return Result.success("分配班级到课程成功", true);
        } else {
            return Result.error("分配班级到课程失败");
        }
    }
    
    /**
     * 更新班级课程关联信息
     *
     * @param courseId     课程ID
     * @param classId      班级ID
     * @param classCourse  班级课程关联信息
     * @return 操作结果
     */
    @PutMapping("/{courseId}/{classId}")
    public Result<Boolean> updateClassCourse(
            @PathVariable String courseId,
            @PathVariable Long classId,
            @RequestBody ClassCourse classCourse) {
        
        // 确保路径中的ID与请求体一致
        classCourse.setCourseId(courseId);
        classCourse.setClassId(classId);
        
        boolean success = classCourseService.updateClassCourse(classCourse);
        if (success) {
            return Result.success("更新班级课程关联成功", true);
        } else {
            return Result.error("更新班级课程关联失败");
        }
    }
    
    /**
     * 更新班级课程状态
     *
     * @param courseId       课程ID
     * @param classId        班级ID
     * @param statusRequest  状态请求
     * @return 操作结果
     */
    @PutMapping("/{courseId}/{classId}/status")
    public Result<Boolean> updateStatus(
            @PathVariable String courseId,
            @PathVariable Long classId,
            @RequestBody StatusUpdateRequest statusRequest) {
        
        if (statusRequest == null || statusRequest.getStatus() == null || statusRequest.getStatus().trim().isEmpty()) {
            return Result.error("状态不能为空");
        }
        
        boolean success = classCourseService.updateStatus(courseId, classId, statusRequest.getStatus());
        if (success) {
            return Result.success("更新班级课程状态成功", true);
        } else {
            return Result.error("更新班级课程状态失败");
        }
    }
    
    /**
     * 删除班级课程关联
     *
     * @param courseId  课程ID
     * @param classId   班级ID
     * @return 操作结果
     */
    @DeleteMapping("/{courseId}/{classId}")
    public Result<Boolean> deleteClassCourse(
            @PathVariable String courseId,
            @PathVariable Long classId) {
        
        boolean success = classCourseService.deleteClassCourse(courseId, classId);
        if (success) {
            return Result.success("删除班级课程关联成功", true);
        } else {
            return Result.error("删除班级课程关联失败");
        }
    }
    
    /**
     * 获取未分配给指定课程的班级ID列表
     *
     * @param courseId 课程ID
     * @return 班级ID列表
     */
    @GetMapping("/unassigned-classes/{courseId}")
    public Result<List<Long>> getUnassignedClasses(@PathVariable String courseId) {
        List<Long> classIds = classCourseService.getClassIdsNotInCourse(courseId);
        return Result.success("获取未分配班级列表成功", classIds);
    }
    
    /**
     * 获取指定课程关联的班级ID列表
     *
     * @param courseId 课程ID
     * @return 班级ID列表
     */
    @GetMapping("/assigned-classes/{courseId}")
    public Result<List<Long>> getAssignedClasses(@PathVariable String courseId) {
        List<Long> classIds = classCourseService.getClassIdsByCourseId(courseId);
        return Result.success("获取已分配班级列表成功", classIds);
    }
    
    /**
     * 获取班级的所有课程
     * @param classId 班级ID
     * @return 课程列表
     */
    @GetMapping("/classes/{classId}/courses")
    public Result<List<ClassCourse>> getCoursesByClassId(@PathVariable Long classId) {
        try {
            List<ClassCourse> courses = classCourseService.getCoursesByClassId(classId);
            return Result.success("获取班级课程列表成功", courses);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取班级课程列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 分配课程到班级
     * @param classCourse 班级课程关联信息
     * @return 操作结果
     */
    @PostMapping("/assign")
    public Result<Boolean> assignCourseToClass(@RequestBody ClassCourse classCourse) {
        boolean result = classCourseService.assignCourseToClass(classCourse);
        if (result) {
            return Result.success("课程分配成功", true);
        } else {
            return Result.error("课程分配失败");
        }
    }
    
    /**
     * 从班级移除课程
     * @param classId 班级ID
     * @param courseId 课程ID
     * @return 操作结果
     */
    @DeleteMapping("/remove")
    public Result<Boolean> removeCourseFromClass(
            @RequestParam Long classId,
            @RequestParam String courseId) {
        boolean result = classCourseService.removeCourseFromClass(classId, courseId);
        if (result) {
            return Result.success("课程移除成功", true);
        } else {
            return Result.error("课程移除失败");
        }
    }
    
    /**
     * 批量分配班级请求
     */
    public static class AssignClassesRequest {
        private List<Long> classIds;
        private String teacherId;
        private String term;
        
        public List<Long> getClassIds() {
            return classIds;
        }
        
        public void setClassIds(List<Long> classIds) {
            this.classIds = classIds;
        }
        
        public String getTeacherId() {
            return teacherId;
        }
        
        public void setTeacherId(String teacherId) {
            this.teacherId = teacherId;
        }
        
        public String getTerm() {
            return term;
        }
        
        public void setTerm(String term) {
            this.term = term;
        }
    }
    
    /**
     * 状态更新请求
     */
    public static class StatusUpdateRequest {
        private String status;
        
        public String getStatus() {
            return status;
        }
        
        public void setStatus(String status) {
            this.status = status;
        }
    }
    
    /**
     * 更新班级课程状态（简化版本）
     *
     * @param request 包含课程ID、班级ID和状态的请求
     * @return 操作结果
     */
    @PutMapping("/status")
    public Result<Boolean> updateCourseStatus(@RequestBody StatusRequest request) {
        if (request == null || request.getCourseId() == null || request.getClassId() == null || 
            request.getStatus() == null || request.getStatus().trim().isEmpty()) {
            return Result.error("参数不完整");
        }
        
        boolean success = classCourseService.updateStatus(request.getCourseId(), request.getClassId(), request.getStatus());
        if (success) {
            return Result.success("更新班级课程状态成功", true);
        } else {
            return Result.error("更新班级课程状态失败");
        }
    }
    
    /**
     * 状态更新请求（包含课程ID和班级ID）
     */
    public static class StatusRequest {
        private String courseId;
        private Long classId;
        private String status;
        
        public String getCourseId() {
            return courseId;
        }
        
        public void setCourseId(String courseId) {
            this.courseId = courseId;
        }
        
        public Long getClassId() {
            return classId;
        }
        
        public void setClassId(Long classId) {
            this.classId = classId;
        }
        
        public String getStatus() {
            return status;
        }
        
        public void setStatus(String status) {
            this.status = status;
        }
    }
} 